import Cookies from 'js-cookie';
import { decrypt, encrypt } from './utils';

const USER_INFO_KEY = 'tangram_user';

let currentTenantId;

export function getUserInfo() {
  const item = Cookies.get(USER_INFO_KEY);
  if (item) {
    return decrypt(item, USER_INFO_KEY);
  }
  return null;
}

export function getUsername() {
  return (getUserInfo() || {}).username;
}

export function removeUserInfo() {
  Cookies.remove(USER_INFO_KEY);
}

export function setUserInfo(user) {
  Cookies.set(USER_INFO_KEY, encrypt(user, USER_INFO_KEY), {
    expires: 30,
  });
}

export function setCurrentTenantId(tenantId) {
  currentTenantId = tenantId;
}

export function getCurrentTenantId() {
  return currentTenantId;
}

export function isMultiTenant() {
  return (getUserInfo() || {}).isMultiTenant;
}
